package com.kungfuhacking.wristbandpro.widget;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.kungfuhacking.wristbandpro.utils.DensityUtil;


/**
 * 自定义电池控件
 */
public class BatteryView extends View {

    private int mPower = 100;
    private int width;
    private int head_width;
    private int head_height;
    private int height;
    private int color_out;
    private int color_inside;
    private int inside_margin;
    private int measureHeight;

    public BatteryView(Context context) {
        super(context);
    }

    public BatteryView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setLength(int width, int height, int head_width, int head_height, int inside_margin) {
        this.width = width;
        this.height = height;
        this.head_width = head_width;
        this.head_height = head_height;
        this.inside_margin = inside_margin;
    }

    public void setColor(int color_out, int color_inside) {
        this.color_out = color_out;
        this.color_inside = color_inside;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

// bv.setLength(35, 15, 5, 10, 1);

//        int parentHeight = DensityUtil.dip2px(getContext(), 35);
//        LogUtil.d(TAG, "parentHeight:" + parentHeight);

//        int battery_left = DensityUtil.dip2px(getContext(), 20);
        int battery_left = 0;
//        int battery_top = (getHeight() - DensityUtil.dip2px(getContext(), height)) / 2;
        int battery_top = 0;

        int battery_width = DensityUtil.dip2px(getContext(), width);
        int battery_height = DensityUtil.dip2px(getContext(), height);

        int battery_head_width = head_width;
        int battery_head_height = head_height;

        int battery_inside_margin = inside_margin;

        //先画外框
        Paint paint = new Paint();
        paint.setColor(color_out);
        paint.setAntiAlias(true);
        paint.setStyle(Style.STROKE);

        Rect rect = new Rect(battery_left, battery_top,
                battery_left + battery_width, battery_top + battery_height);
        canvas.drawRect(rect, paint);

        float power_percent = mPower / 100.0f;

        Paint paint2 = new Paint(paint);
        paint2.setColor(color_inside);
        paint2.setStyle(Style.FILL);
        //画电量
        if (power_percent != 0) {
            int p_left = battery_left + battery_inside_margin;
            int p_top = battery_top + battery_inside_margin;
            int p_right = p_left - battery_inside_margin + (int) ((battery_width - battery_inside_margin) * power_percent);
            int p_bottom = p_top + battery_height - battery_inside_margin * 2;
            Rect rect2 = new Rect(p_left, p_top, p_right, p_bottom);
            canvas.drawRect(rect2, paint2);
        }

        //画电池头
        int h_left = battery_left + battery_width;
        int h_top = battery_top + battery_height / 2 - battery_head_height / 2;
        int h_right = h_left + battery_head_width;
        int h_bottom = h_top + battery_head_height;
        Rect rect3 = new Rect(h_left, h_top, h_right, h_bottom);
        canvas.drawRect(rect3, paint);
    }

    private String TAG = getClass().getSimpleName();

//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//
//        int measureWidth = measureWidth(widthMeasureSpec);
//        measureHeight = measureHeight(heightMeasureSpec);
//
//        LogUtil.d(TAG, "电池控件所在的父控件的高度:" + measureHeight + ",宽度:" + measureWidth
//                + "父控件的高度");
//
//        // 设置自定义的控件MyViewGroup的大小
//        setMeasuredDimension(measureWidth, measureHeight);
//
//    }
//
//    private int measureWidth(int pWidthMeasureSpec) {
//        int result = 0;
//        int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);// 得到模式
//        int widthSize = MeasureSpec.getSize(pWidthMeasureSpec);// 得到尺寸
//
//        switch (widthMode) {
//            /**
//             * mode共有三种情况，取值分别为MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY,
//             * MeasureSpec.AT_MOST。
//             *
//             *
//             * MeasureSpec.EXACTLY是精确尺寸，
//             * 当我们将控件的layout_width或layout_height指定为具体数值时如andorid
//             * :layout_width="50dip"，或者为FILL_PARENT是，都是控件大小已经确定的情况，都是精确尺寸。
//             *
//             *
//             * MeasureSpec.AT_MOST是最大尺寸，
//             * 当控件的layout_width或layout_height指定为WRAP_CONTENT时
//             * ，控件大小一般随着控件的子空间或内容进行变化，此时控件尺寸只要不超过父控件允许的最大尺寸即可
//             * 。因此，此时的mode是AT_MOST，size给出了父控件允许的最大尺寸。
//             *
//             *
//             * MeasureSpec.UNSPECIFIED是未指定尺寸，这种情况不多，一般都是父控件是AdapterView，
//             * 通过measure方法传入的模式。
//             */
//            case MeasureSpec.AT_MOST:
//            case MeasureSpec.EXACTLY:
//                result = widthSize;
//                break;
//        }
//        return result;
//    }
//
//    private int measureHeight(int pHeightMeasureSpec) {
//        int result = 0;
//
//        int heightMode = MeasureSpec.getMode(pHeightMeasureSpec);
//        int heightSize = MeasureSpec.getSize(pHeightMeasureSpec);
//
//        switch (heightMode) {
//            case MeasureSpec.AT_MOST:
//            case MeasureSpec.EXACTLY:
//                result = heightSize;
//                break;
//        }
//        return result;
//    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
//        LogUtil.e(TAG, "onLayout");
//        LogUtil.d(TAG, changed + ",控件的left:" + left + ",top:" + top +
//                ",right:" + right + ",bottom:" + bottom);
//
//        int parentHeight = DensityUtil.dip2px(getContext(), 50);
//
//        LogUtil.d(TAG, "getwidth:" + getWidth() + ",getheitght:" + getHeight());

        super.onLayout(changed, left, top, right, bottom);
    }

    public void setPower(int power) {
        mPower = power;
        if (mPower < 0) {
            mPower = 0;
        }
        invalidate();
    }
}
